#include "data_record.h"
#include <stdarg.h>
#include <string.h>
#include <stdint.h>

#define PRINTF(...)     // printf(__VA_ARGS__)

static int open_file(data_record_t v, const char *file_name, const char *mode)
{
    PRINTF("enter: %s\n", __func__);
    PRINTF("ins_name: %s\n", v->ins_name);
    if (v->enable_record){
        if(v->fp != NULL){
            PRINTF ( "%s already opened, v->fp: %p\n", file_name, v->fp );
            return -1;
        }
        v->fp = fopen ( file_name, mode );
        if ( v->fp == NULL ) {
            PRINTF ( "open %s err\n", file_name );
            return -1;
        }

        size_t name_len = strlen(file_name);
        if (name_len > DR_FILE_NAME_LEN){
            name_len = DR_FILE_NAME_LEN;
        }
        memcpy(v->file_name, file_name, strlen(file_name));
    }

	return 0;
}

int dr_open_file(data_record_t v, const char *file_name)
{
    PRINTF("enter: %s\n", __func__);
    v->open_mode = MODE_ASCII;
    return open_file(v, file_name, "w+");
}

int dr_open_bin_create(data_record_t v, const char *file_name)
{
    PRINTF("enter: %s\n", __func__);
    v->open_mode = MODE_BIN;
    return open_file(v, file_name, "wb+");
}

int dr_open_bin_read(data_record_t v, const char *file_name)
{
    PRINTF("enter: %s\n", __func__);
    v->open_mode = MODE_BIN;
    return open_file(v, file_name, "rb");
}


void record_data(data_record_t v, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
    PRINTF("enter: %s\n", __func__);
    PRINTF("v->ins: %s\n", v->ins_name);
	if (v->enable_record){
        if (v->fp != NULL){
            switch(v->open_mode){
                case MODE_ASCII:
                    vfprintf(v->fp, fmt, args);
                break;

//                case MODE_BIN:;
//                    vsnPRINTF(buf, sizeof(buf), fmt, args);
//                    size_t len = strlen(buf);
//                    fwrite(buf, sizeof(buf[0]), len, v->fp);
//                break;
                default:
                    break;
            }

        }else{
            PRINTF("not open file:---- %s\n", v->ins_name);
        }
	}


    va_end(args);
}

//static uint8_t bin_buf[1024];
//static uint16_t bin_buf_idx;
void record_bin_data(data_record_t v, const void *ptr, size_t size, size_t nmemb)
{
    PRINTF("enter: %s\n", __func__);
	if (v->enable_record){
        if(v->fp == NULL){
             PRINTF("not open file:---- %s\n", v->ins_name);
             return;
        }
        fwrite(ptr, size, nmemb, v->fp);
	}
}


size_t read_bin_data(data_record_t v, void *ptr, size_t size, size_t nmemb)
{
    PRINTF("enter: %s\n", __func__);
    size_t read_cnt = 0;(void)read_cnt;
	if (v->enable_record){
        if(v->fp == NULL){
             PRINTF("not open file:---- %s\n", v->ins_name);
             return 0;
        }
        read_cnt = fread(ptr, size, nmemb, v->fp);
        PRINTF("read_cnt: %u\n", read_cnt);
	}
    return read_cnt;
}


int dr_close_file(data_record_t v)
{
    PRINTF("enter: %s\n", __func__);
    if(v->fp == NULL){
         PRINTF("not open file:---- %s\n", v->ins_name);
         return -1;
    }
    int res = fclose(v->fp);
    v->fp = NULL;
    PRINTF("v->fp: %p\n", v->fp);
    return res;
}



